# -*- coding: utf-8 -*-#
#-------------------------------------------------------------------------------
# 建立者:        黄周云  
# Name:         daima
# Description:
# Author:       ASUS
# Date:         2019/4/29
#-------------------------------------------------------------------------------



import requests
from pyquery import PyQuery as pq
import os

class xiaoshuo(object):
    #请求第一个页面，拿到小说名，小说详情链接，创建小说名的文件夹
    def firstFile(self):
        response = requests.get('https://www.qidian.com/all')
        html = pq(response.text)
        xsName_list = html('.all-img-list.cf li .book-mid-info h4 a').text()
        xsName_url = html('.all-img-list.cf li .book-mid-info h4 a').attr.href
        for xsName,xsUrl in zip(xsName_list,xsName_url):
            if os.path.exists(xsName) == False:
                os.mkdir(xsName)
            self.next_Files(xsName,xsUrl)

#请求第二个页面，拿到章节名，章节链接。
    def next_Files(self,xsName,xsUrl):
        litleurl = 'http:' + xsUrl
        response2 = requests.get(litleurl)
        chapterName_html= pq(response2.text)
        chapterName_list = chapterName_html('.cf li a').text()
        chapterSrc_list = chapterName_html('.cf li a').attr.href
        for chapterName,chapterSrc in zip(chapterName_list,chapterSrc_list):
            self.finally_Files(chapterName,chapterSrc,xsName)

#请求第三个页面，拿到小说正文数据，保存数据
    def finally_Files(self,chapterName,chapterSrc,xsName):
        zhengwenurl = 'http:'+ chapterSrc
        response3 = requests.get(zhengwenurl)
        zwNengrong = pq(response3.text)
        zhengwen = '\n'.join(zwNengrong('.read-content.j_readContent p').text())
        filename = xsName + "//" + chapterName + '.txt'
        print('正在爬取：' +filename)
        with open(filename,'w',encoding='utf-8') as f:
            f.write(zhengwen)


if __name__ == '__main__':
    xshuo = xiaoshuo()
    xshuo.firstFile()


